package com.dominator.serviceImpl;

import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.utils.SystemUtils;
import com.dominator.enums.ReqEnums;
import com.dominator.mybatis.dao.T_lot_orderPO;
import com.dominator.repository.DaoAccount;
import com.dominator.repository.DaoLotOrder;
import com.dominator.repository.DaoUser;
import com.dominator.service.ApiAccountService;
import com.dominator.service.ApiLotteryService;
import com.dominator.service.ApiOrderService;
import com.dominator.utils.api.ApiMessage;
import com.dominator.utils.api.ApiMessageUtil;
import com.dominator.utils.api.ApiUtils;
import com.dominator.utils.exception.ApiException;
import com.dominator.utils.system.PropertiesLoader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class ApiLotteryServiceImpl implements ApiLotteryService {

    public static PropertiesLoader propertiesLoader = new PropertiesLoader("sysconfig.properties");


    @Autowired
    ApiOrderService apiOrderService;
    @Autowired
    DaoUser userDao;
    @Autowired
    DaoAccount accountDao;
    @Autowired
    DaoLotOrder lotOrderDao;
    @Autowired
    ApiAccountService apiAccountService;

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public ApiMessage betDeductPoints(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto, "phoneNum,orderNo,typeId");
        T_lot_orderPO lotOrderPO = lotOrderDao.getLotOrderByOrderNo(dto.getString("orderNo"));
        if (SystemUtils.isEmpty(lotOrderPO)) {
            throw new ApiException(ReqEnums.LOT_IS_NOT_EXSIT);
        }else if(lotOrderPO.getWin_status()==1){
            throw new ApiException(ReqEnums.LOT_IS_WIN);
        }
        switch (dto.getInteger("typeId")){
            case 5:
                if (lotOrderPO.getStatus()==1){
                    throw new ApiException(ReqEnums.LOT_IS_PAY_SUCCESS);
                }
                break;
            case 7:
                if (lotOrderPO.getStatus()==0){
                    throw new ApiException(ReqEnums.LOT_IS_NOT_PAY);
                }else if (lotOrderPO.getStatus()==2){
                    throw new ApiException(ReqEnums.LOT_IS_PAY_FAIED);
                }
                break;
            default:
                throw new ApiException(ReqEnums.ORDERTYPE_IS_NOT_EXSIT);
        }
        //统一下单
        dto.put("points", lotOrderPO.getPoints());
        dto.put("sn",lotOrderPO.getOrder_no());
        apiOrderService.createOrder(dto);
        return ApiMessageUtil.success();
    }


    @Override
    public ApiMessage betBonusMoney(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto, "phoneNum,orderNo,typeId");
        T_lot_orderPO lotOrderPO = lotOrderDao.getLotOrderByOrderNo(dto.getString("orderNo"));
        if (SystemUtils.isEmpty(lotOrderPO)) {
            throw new ApiException(ReqEnums.LOT_IS_NOT_EXSIT);
        }else if(lotOrderPO.getWin_status()==1){
            throw new ApiException(ReqEnums.LOT_IS_WIN);
        }
        dto.put("money", lotOrderPO.getWin_num());
        dto.put("sn",lotOrderPO.getOrder_no());
        apiOrderService.createOrder(dto);
        return ApiMessageUtil.success();
    }
}
